<?php

/**
 * 主题内容管理
 */
namespace app\topics\model;

use app\system\model\SystemModel;

class TopicsDetailFaqModel extends SystemModel {

    protected $infoModel = [
        'pri' => 'id',
        'validate' => [
            'markdown' => [
                'empty' => ['', '请输入主题内容！', 'must', 'all'],
            ],
            'detail_id' => [
                'empty' => ['', '主题ID不正确!', 'must', 'all'],
            ],
        ],
        'format' => [
            'detail_id' => [
                'function' => ['intval', 'all'],
            ],
            'html' => [
                'callback' => ['parseHtml', 'all']
            ]
        ],
        'into' => 'detail_id,markdown,type',
        'out' => '',
    ];

    public function parseHtml($field, $filedData) {
        $data = $this->getData();
        $html = target('topics/TopicsDetail')->changeHtml($data['markdown']);
        //$html = str_replace("\n", '<br>', $html);
        return $html;
    }

    protected function base($where) {
        return $this->table('topics_detail(A)')
            ->join('topics_node(B)', ['B.node_id', 'A.node_id'])
            ->join('topics_user(C)', ['C.user_id', 'A.user_id'])
            ->join('topics_detail_faq(D)', ['D.detail_id', 'A.detail_id'])
            ->field(['A.*', 'B.name(node_name)', 'C.username(show_name)', 'D.answer', 'C.avatar'])
            ->where((array)$where);
    }

    public function loadList($where = array(), $limit = 0, $order = '') {
        $list = $this->base($where)
            ->limit($limit)
            ->order($order)
            ->select();

        $typeList = target('topics/Topics', 'service')->getTypeAll();
        foreach ($list as $key => $vo) {
            $list[$key]['avatar'] = $vo['avatar'] ? $vo['avatar'] : ROOT_URL . '/public/topics/images/logo.png';
            $list[$key]['url'] = url($typeList[$vo['type']]['info_url'], ['id' => $vo['detail_id']]);
        }
        return $list;
    }

    public function countList($where = array()) {
        return $this->base($where)->count();
    }


    public function saveData($type = 'add', $data = []) {
        $this->beginTransaction();
        $id = 0;
        if ($type == 'add') {
            $data['type'] = 'faq';
            $id = target('topics/TopicsDetail')->saveData('add', $data);
            if (!$id) {
                $this->rollBack();
                $this->error = target('topics/TopicsDetail')->getError();
                return false;
            }
            $data['detail_id'] = $id;
            $status = parent::saveData('add', $data);
            if (!$status) {
                $this->rollBack();
                return false;
            }
        }
        if ($type == 'edit') {
            $status = target('topics/TopicsDetail')->saveData('edit', $data);
            if (!$status) {
                $this->rollBack();
                $this->error = target('topics/TopicsDetail')->getError();
                return false;
            }
            $info = $this->getWhereInfo(['has_id' => $data['detail_id']]);
            $data['id'] = $info['id'];
            $status = parent::saveData('edit', $data);
            if (!$status) {
                $this->rollBack();
                return false;
            }
            $id = $data['detail_id'];
        }

        $this->commit();
        return $id;
    }


}